package view;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.WindowConstants;

import controller.Maquina;
import controller.exception.MaquinaInstantiationException;

/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class Juego extends javax.swing.JPanel {
	private static Juego instancia;
	private JButton jButton1;
	private JLabel jLabel1;
	private JButton cerrarSesion;
	private JFrame feedback;
	private JLabel saldo;
	private JLabel jLabel2;

	private Juego() {
		super();
		initGUI();
	}
	
	public static Juego getInstance() {
		if (instancia == null) {
			instancia = new Juego();
		}
		return instancia;
	}
	
	private void initGUI() {
		try {
			this.setPreferredSize(new java.awt.Dimension(246, 237));
			this.setLayout(null);
			this.add(getCerrarSesion());
			this.add(getJButton1());
			this.add(getJLabel1());
			this.add(getJLabel2());
			this.add(getSaldo());
			actualizarCredito();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private JButton getCerrarSesion() {
		if(cerrarSesion == null) {
			cerrarSesion = new JButton();
			cerrarSesion.setText("Cerrar Sesion");
			cerrarSesion.setBounds(12, 156, 211, 60);
			cerrarSesion.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					try {
						Maquina.getInstancia().cerrarSesion();
						System.exit(0);
					} catch (MaquinaInstantiationException e1) {
						e1.printStackTrace();
					}
				}
			});
		}
		return cerrarSesion;
	}
	
	private JButton getJButton1() {
		if(jButton1 == null) {
			jButton1 = new JButton();
			jButton1.setText("Jugar!");
			jButton1.setBounds(12, 85, 211, 60);
			jButton1.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					try {
						Maquina maquina = Maquina.getInstancia();
						boolean resultado = maquina.jugar();
						System.out.println("Nuevo credito: " + maquina.getCredito());
						if (resultado) {
							// Gano!
							System.out.println("El usuario gano");
							Juego.getInstance().mostrarVentanaDeFeedback(JuegoFeedbackGano.getInstance());
						} else {
							// Perdio..
							System.out.println("El usuario perdio");
							Juego.getInstance().mostrarVentanaDeFeedback(JuegoFeedbackNoGano.getInstance());
						}
					} catch (MaquinaInstantiationException e1) {
						e1.printStackTrace();
					}
				}
			});
		}
		return jButton1;
	}
	
	private JLabel getJLabel1() {
		if(jLabel1 == null) {
			jLabel1 = new JLabel();
			jLabel1.setText("A probar suerte!");
			jLabel1.setBounds(12, 12, 171, 15);
		}
		return jLabel1;
	}
	
	public void mostrarVentanaDeFeedback(JPanel panel) {
		feedback = new JFrame();
		feedback.getContentPane().add(panel);
		feedback.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		feedback.pack();
		feedback.setVisible(true);
	}

	public void cerrarVentanaDeFeedback() {
		feedback.getContentPane().removeAll();
		feedback.setVisible(false);
		actualizarCredito();
	}
	
	private JLabel getJLabel2() {
		if(jLabel2 == null) {
			jLabel2 = new JLabel();
			jLabel2.setText("Su saldo: $");
			jLabel2.setBounds(12, 49, 82, 15);
		}
		return jLabel2;
	}
	
	private JLabel getSaldo() {
		if(saldo == null) {
			saldo = new JLabel();
			saldo.setText("0");
			saldo.setBounds(87, 49, 99, 15);
		}
		return saldo;
	}

	private void actualizarCredito() {
		try {
			saldo.setText(Float.toString(Maquina.getInstancia().getCredito()));
		} catch (Exception e) {
			System.out.println("No se pudo obtener el saldo.");
		}
		
	}
}
